<?php

// file_put_contents("git-webhook_log.txt", 'test-webhooks', FILE_APPEND);//写入日志到log文件中
// exit('webhook');
$config = array (
		0 => array (
				"ref" => "refs/heads/master",
				"savePath" => "/var/www/html/git.7b620.cn/",
				"gitPath" => "origin master" 
		),
		1 => array (
				"ref" => "refs/heads/dev",
				"savePath" => "/var/www/html/dev.7b620.cn/",
				"gitPath" => "origin dev" 
		) 
);

// git webhook 自动部署脚本
// 项目存放物理路径,第一次clone时,必须保证该目录为空
// $savePath = "/var/www/html/git.7b620.cn/";
// $gitPath = "origin master"; // 代码仓库

$email = "327907762@qq.com"; // 用户仓库邮箱
                             // $name = "admin";//仓库用户名,一般和邮箱一致即可
$name = "327907762@qq.com"; // 仓库用户名,一般和邮箱一致即可

$isClone = true; // 设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.
                 
// 如果已经clone过,则直接拉去代码
if ($isClone) {
	$requestBody = file_get_contents ( "php://input" ); // handle post data
	if (empty ( $requestBody )) {
		file_put_contents ( "git-webhook_log_1.txt", "send fail" . PHP_EOL, FILE_APPEND ); // 写入日志到log文件中
	}
	file_put_contents ( "git-webhook_log_2.txt", $requestBody . PHP_EOL, FILE_APPEND ); // 写入日志到log文件中
	                                                                                    // 解析Git服务器通知过来的JSON信息
	$content = json_decode ( $requestBody, true );
	
	foreach ( $config as $k => $v ) {
		if ($content ['ref'] == $v ['ref'] && $content ['total_commits_count'] > 0) {
			$savePath = $v ['savePath'];
			$gitPath = $v ['gitPath'];
			$res = PHP_EOL . "pull start --------" . PHP_EOL;
			$res_log = '-------------------------' . PHP_EOL;
			$res_log .= "cd {$savePath} && git pull {$gitPath}" . PHP_EOL;
			
			$res .= shell_exec ( "cd {$savePath} && git pull {$gitPath}" ); // 拉去代码
			$res_log .= $content ['user_name'] . ' 在' . date ( 'Y-m-d H:i:s' ) . '向' . $content ['repository'] ['name'] . '项目的' . $content ['ref'] . '分支push了' . $content ['total_commits_count'] . '个commit：';
			
			$res_log .= PHP_EOL."chown -R www.www {$savePath}" . PHP_EOL;
			$res .= shell_exec ( "chown -R www.www {$savePath}" ) . PHP_EOL; // 修改web路径的权限
			
			$res_log .= $res . PHP_EOL;
			$res_log .= "pull end --------" . PHP_EOL;
			
			file_put_contents ( "git-webhook_log_3.txt", $res_log, FILE_APPEND ); // 写入日志到log文件中
		}
	}
	
	// // 若是主分支且提交数大于0
	// if ($content ['ref'] == 'refs/heads/master' && $content ['total_commits_count'] > 0) {
	
	// $res = PHP_EOL . "pull start --------" . PHP_EOL;
	// $res .= shell_exec ( "cd {$savePath} && git pull {$gitPath}" ); // 拉去代码
	// $res_log = '-------------------------' . PHP_EOL;
	// $res_log .= $content ['user_name'] . ' 在' . date ( 'Y-m-d H:i:s' ) . '向' . $content ['repository'] ['name'] . '项目的' . $content ['ref'] . '分支push了' . $content ['total_commits_count'] . '个commit：';
	// $res_log .= $res . PHP_EOL;
	// $res_log .= "pull end --------" . PHP_EOL;
	// $res .= shell_exec ( "chown -R www.www {$savePath}" ) . PHP_EOL; // 修改web路径的权限
	
	// file_put_contents ( "git-webhook_log_3.txt", $res_log, FILE_APPEND ); // 写入日志到log文件中
	// }
} else {
	$res = "clone start --------" . PHP_EOL;
	// 注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码
	// shell_exec 需要root的权限
	$res .= shell_exec ( "git config --global user.email {$email}}" ) . PHP_EOL;
	$res .= shell_exec ( "git config --global user.name {$name}}" ) . PHP_EOL;
	$res .= shell_exec ( "git clone {$gitPath} {$savePath}" ) . PHP_EOL;
	$res .= "clone end --------" . PHP_EOL;
	$res .= shell_exec ( "chown -R www.www {$savePath}" ) . PHP_EOL; // 修改web路径的权限
	file_put_contents ( "git-webhook_log.txt", $res, FILE_APPEND ); // 写入日志到log文件中
}
